package com.yangyouli.unity_demo_server.controller;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.alibaba.fastjson.JSONObject;
import com.yangyouli.unity_demo_server.annotation.Inject;
import com.yangyouli.unity_demo_server.annotation.ResponseClass;
import com.yangyouli.unity_demo_server.annotation.ResponseMethod;
import com.yangyouli.unity_demo_server.entity.FriendsVO;
import com.yangyouli.unity_demo_server.server.DataSave;
import com.yangyouli.unity_demo_server.server.Server;
import com.yangyouli.unity_demo_server.server.serializeModel.PrivateWords;
import com.yangyouli.unity_demo_server.server.serializeModel.PrivateWordsList;
import com.yangyouli.unity_demo_server.service.FriendsService;
import com.yangyouli.unity_demo_server.util.ThreadLocalHelper;

@ResponseClass("privateWords")
public class PrivateWordsController {
	/**
	 * 客户端对应的相应类名，玩家在线收到好友私信时，通知他刷新消息列表，返回发送私信的用户id 用Set集合封装
	 */
	static final String REFLUSH_NEWS_RESPONSE_CLASS_NAME = "HasNewPrivateWordsRequest";

	@Inject
	private FriendsService friendsService;
	
	/**
	  * 给某人发送私信，参数为 PrivateWords 的 json 字符串
	 * @param data
	 * @return
	 */
	@ResponseMethod("send")
	public String sendPrivateWords (String data) {
		PrivateWords p = JSONObject.parseObject(data, PrivateWords.class);
		p.setSendTime(new Date().getTime());
		p.setExpireTime(DataSave.PRIVATE_WORDS_EXPIRE_TIME);
		DataSave.getInstance().addPrivateWordsMessage(p);
		String result = JSONObject.toJSONString(p);
		Server server = ThreadLocalHelper.getClient().getServer();
		if (server.isUserOnline(p.getReciveUserId())) {
			ThreadLocalHelper.delayResponse(result, server.getLoginUserClient(p.getReciveUserId()));
		}
		return result;
	}
	
	/**
	 * 得到特定接收者的与所有存在未读消息的用户的 id
	 * @param data
	 */
	@ResponseMethod("hasNotReadPrivateWordsUserId")
	public String hasNotReadPrivateWordsUserId (String data) {
		int userId = ThreadLocalHelper.getClient().getUserId();
		Set<Integer> idSet = DataSave.getInstance().getHasNotReadPrivateWordsUserId(userId);
		if (idSet == null) {
			idSet = new HashSet<>();
		}
		return JSONObject.toJSONString(idSet);
	}
	
	/**
	 * 设置与某个好友的所有消息为已读状态
	 * @param data 好友的 id
	 */
	@ResponseMethod("readAllOfSomeOne")
	public void readAllOfSomeOne (String data) {
		int userId = ThreadLocalHelper.getClient().getUserId();
		List<PrivateWords> l = DataSave.getInstance().getPrivateWordsMessage(userId, Integer.valueOf(data));
		for (PrivateWords p : l) {
			p.setRead(true);
		}
	}
	
	/**
	 * 得到特定用户与某个用户的聊天信息，参数为某个用户的 id
	 * @param data
	 * @return PrivateWordsList
	 */
	@ResponseMethod("getWithSomeOne")
	public String getPrivateWordsWithSomeOne (String data) {
		int userId = ThreadLocalHelper.getClient().getUserId();
		List<PrivateWords> l = DataSave.getInstance().getPrivateWordsMessage(userId, Integer.valueOf(data));
		FriendsVO recive = friendsService.findByUserId(userId);
		FriendsVO send = friendsService.findByUserId(Integer.valueOf(data));
		PrivateWordsList pl = new PrivateWordsList();
		pl.setSend(send);
		pl.setRecive(recive);
		pl.setPrivateWordsList(l);
		return JSONObject.toJSONString(pl);
	}
}
